<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>WindowsVBScript | lilei</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/images/icons/logo.png">
    <link rel="apple-touch-icon" href="/images/icons/logo.png">
    <link rel="manifest" href="/manifest.json">
    <script type="text/javascript" src="/assets/js/load.js"></script>
    <meta name="description" content="个人网站">
    <meta name="twitter:title" content="WindowsVBScript">
    <meta name="twitter:description" content="">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:url" content="http://www.lileiblog.xyz/Shell/WindowsVBScript.html">
    <meta property="og:type" content="article">
    <meta property="og:title" content="WindowsVBScript">
    <meta property="og:description" content="">
    <meta property="og:url" content="http://www.lileiblog.xyz/Shell/WindowsVBScript.html">
    <meta property="og:site_name" content="lilei">
    <meta itemprop="name" content="WindowsVBScript">
    <meta itemprop="description" content="">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/images/icons/logo.png">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="keywords" content="lilei,vue,vuepress,vuepress-theme,theme,主题,vuepress主题,blog
        ,vuepress-blog,java,python,shell,sql,golang,script,shell script,nginx,windows,去广告,androd,google drive
        ,one drive,idea,eclipse,git,小程序">
    <meta name="referrer" content="never">
    
    <link rel="preload" href="/assets/css/0.styles.8932179b.css" as="style"><link rel="preload" href="/assets/js/app.269009d6.js" as="script"><link rel="preload" href="/assets/js/3.974b9720.js" as="script"><link rel="preload" href="/assets/js/64.aa854ed7.js" as="script"><link rel="prefetch" href="/assets/js/10.5915e99b.js"><link rel="prefetch" href="/assets/js/11.ff732762.js"><link rel="prefetch" href="/assets/js/12.8bb86d91.js"><link rel="prefetch" href="/assets/js/13.18bde5b0.js"><link rel="prefetch" href="/assets/js/14.314fae34.js"><link rel="prefetch" href="/assets/js/15.0a4ff48f.js"><link rel="prefetch" href="/assets/js/16.ffaf1503.js"><link rel="prefetch" href="/assets/js/17.49a31a6e.js"><link rel="prefetch" href="/assets/js/18.7e6443bf.js"><link rel="prefetch" href="/assets/js/19.6880cc9a.js"><link rel="prefetch" href="/assets/js/20.1fb3f255.js"><link rel="prefetch" href="/assets/js/21.0cd39eea.js"><link rel="prefetch" href="/assets/js/22.392d85cb.js"><link rel="prefetch" href="/assets/js/23.f50adb15.js"><link rel="prefetch" href="/assets/js/24.43e324e1.js"><link rel="prefetch" href="/assets/js/25.e3663205.js"><link rel="prefetch" href="/assets/js/26.da141fb8.js"><link rel="prefetch" href="/assets/js/27.6c05235a.js"><link rel="prefetch" href="/assets/js/28.c124245a.js"><link rel="prefetch" href="/assets/js/29.d185870d.js"><link rel="prefetch" href="/assets/js/30.8587d422.js"><link rel="prefetch" href="/assets/js/31.dc382b56.js"><link rel="prefetch" href="/assets/js/32.20ba32e9.js"><link rel="prefetch" href="/assets/js/33.6ecd7de9.js"><link rel="prefetch" href="/assets/js/34.2b91c13a.js"><link rel="prefetch" href="/assets/js/35.66ed0733.js"><link rel="prefetch" href="/assets/js/36.ce7bca84.js"><link rel="prefetch" href="/assets/js/37.d00bb5ac.js"><link rel="prefetch" href="/assets/js/38.c3ecbb6f.js"><link rel="prefetch" href="/assets/js/39.308f4175.js"><link rel="prefetch" href="/assets/js/4.520f6ecc.js"><link rel="prefetch" href="/assets/js/40.f4b98bc2.js"><link rel="prefetch" href="/assets/js/41.95fae64f.js"><link rel="prefetch" href="/assets/js/42.fe4f877b.js"><link rel="prefetch" href="/assets/js/43.ca8f222e.js"><link rel="prefetch" href="/assets/js/44.4d4ee79e.js"><link rel="prefetch" href="/assets/js/45.79178b33.js"><link rel="prefetch" href="/assets/js/46.c3817d0b.js"><link rel="prefetch" href="/assets/js/47.b8252443.js"><link rel="prefetch" href="/assets/js/48.df9d5d3b.js"><link rel="prefetch" href="/assets/js/49.d812fdb2.js"><link rel="prefetch" href="/assets/js/5.f1a24a16.js"><link rel="prefetch" href="/assets/js/50.55cfcaa8.js"><link rel="prefetch" href="/assets/js/51.b8608b02.js"><link rel="prefetch" href="/assets/js/52.52c5e00d.js"><link rel="prefetch" href="/assets/js/53.3a9bd5fb.js"><link rel="prefetch" href="/assets/js/54.a4240c13.js"><link rel="prefetch" href="/assets/js/55.01c4b876.js"><link rel="prefetch" href="/assets/js/56.f29549a3.js"><link rel="prefetch" href="/assets/js/57.01438a04.js"><link rel="prefetch" href="/assets/js/58.29bb4626.js"><link rel="prefetch" href="/assets/js/59.e8b89408.js"><link rel="prefetch" href="/assets/js/6.8c478b39.js"><link rel="prefetch" href="/assets/js/60.bedb5381.js"><link rel="prefetch" href="/assets/js/61.bdfbe584.js"><link rel="prefetch" href="/assets/js/62.cb825d1a.js"><link rel="prefetch" href="/assets/js/63.a9516572.js"><link rel="prefetch" href="/assets/js/65.285f883d.js"><link rel="prefetch" href="/assets/js/66.12849f37.js"><link rel="prefetch" href="/assets/js/67.c18b00e4.js"><link rel="prefetch" href="/assets/js/68.fa59daa9.js"><link rel="prefetch" href="/assets/js/69.e9d5dda4.js"><link rel="prefetch" href="/assets/js/7.cfb0d0ea.js"><link rel="prefetch" href="/assets/js/70.db126cce.js"><link rel="prefetch" href="/assets/js/71.6acb04cd.js"><link rel="prefetch" href="/assets/js/72.7ff0ab11.js"><link rel="prefetch" href="/assets/js/73.9a66da79.js"><link rel="prefetch" href="/assets/js/74.0a38a030.js"><link rel="prefetch" href="/assets/js/75.7b32dbb3.js"><link rel="prefetch" href="/assets/js/8.cb9c08e5.js"><link rel="prefetch" href="/assets/js/9.ad652cfe.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.110a1065.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8932179b.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/images/icons/logo.png" alt="lilei" class="logo"> <span class="site-name can-hide">lilei</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/Form/" class="nav-link">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link router-link-active">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/Form/" class="nav-link">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link router-link-active">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Shell</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Shell/" aria-current="page" class="sidebar-link">Shell</a></li><li><a href="/Shell/PowerShell.html" class="sidebar-link">PowerShell</a></li><li><a href="/Shell/ShellScript.html" class="sidebar-link">ShellScript</a></li><li><a href="/Shell/ShellWindows.html" class="sidebar-link">ShellWindows</a></li><li><a href="/Shell/Shell命令.html" class="sidebar-link">Shell命令</a></li><li><a href="/Shell/WindowsBatch.html" class="sidebar-link">WindowsBatch</a></li><li><a href="/Shell/WindowsJScript.html" class="sidebar-link">WindowsJScript</a></li><li><a href="/Shell/WindowsScript.html" class="sidebar-link">WindowsScript</a></li><li><a href="/Shell/WindowsVBScript.html" aria-current="page" class="active sidebar-link">WindowsVBScript</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Shell/WindowsVBScript.html#flag" class="sidebar-link">Flag</a></li><li class="sidebar-sub-header"><a href="/Shell/WindowsVBScript.html#特殊符号" class="sidebar-link">特殊符号</a></li><li class="sidebar-sub-header"><a href="/Shell/WindowsVBScript.html#函数封装" class="sidebar-link">函数封装</a></li><li class="sidebar-sub-header"><a href="/Shell/WindowsVBScript.html#vba" class="sidebar-link">VBA</a></li></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="windowsvbscript"><a href="#windowsvbscript" class="header-anchor">#</a> WindowsVBScript</h1> <p></p><div class="table-of-contents"><ul><li><a href="#flag">Flag</a></li><li><a href="#特殊符号">特殊符号</a></li><li><a href="#函数封装">函数封装</a><ul><li><a href="#自动关闭弹窗">自动关闭弹窗</a></li><li><a href="#数组转换为字符串">数组转换为字符串</a></li><li><a href="#获取对象的属性和值">获取对象的属性和值</a></li><li><a href="#获取系统信息">获取系统信息</a></li><li><a href="#隐藏窗口运行">隐藏窗口运行</a></li><li><a href="#查看进程是否存在">查看进程是否存在</a></li><li><a href="#ping">Ping</a></li><li><a href="#监视网络连接">监视网络连接</a></li><li><a href="#监视网络断开">监视网络断开</a></li><li><a href="#设置壁纸">设置壁纸</a></li><li><a href="#刷新桌面">刷新桌面</a></li><li><a href="#字符编码转换">字符编码转换</a></li><li><a href="#选择文件对话框">选择文件对话框</a></li><li><a href="#文件编码转换">文件编码转换</a></li></ul></li><li><a href="#vba">VBA</a></li></ul></div><p></p> <h2 id="flag"><a href="#flag" class="header-anchor">#</a> Flag</h2> <ul><li>批处理之家 <a href="http://www.bathome.net" target="_blank" rel="noopener noreferrer">http://www.bathome.net<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>中国DOS联盟 <a href="http://cndos.fam.cx/forum/forumdisplay.php?fid=23" target="_blank" rel="noopener noreferrer">DOS批处理 &amp; 脚本技术（批处理室）<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li><a href="https://docs.microsoft.com/zh-cn/previous-versions//t0aew7h6(v=vs.85)" target="_blank" rel="noopener noreferrer">VBScript<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://docs.microsoft.com/zh-cn/dotnet/visual-basic" target="_blank" rel="noopener noreferrer">Visual Basic 指南<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://docs.microsoft.com/zh-cn/previous-versions//d1wf56tt%28v%3dvs.85%29" target="_blank" rel="noopener noreferrer">VBScript语言参考<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://docs.microsoft.com/zh-cn/previous-versions//0ad0dkea%28v%3dvs.85%29" target="_blank" rel="noopener noreferrer">VBScript基础知识<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.tcoline.com/resource/vbs/top_vbs_0.htm" target="_blank" rel="noopener noreferrer">VBScript 语言<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://code.ziqiangxuetang.com/vbscript/vbscript-tutorial.html" target="_blank" rel="noopener noreferrer">VBScript 教程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.w3school.com.cn/vbscript/vbscript_ref_functions.asp" target="_blank" rel="noopener noreferrer">VBScript 函数<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/MishaVernik/WScirpt" target="_blank" rel="noopener noreferrer">https://github.com/MishaVernik/WScirpt<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li><a href="https://www.runoob.com/vbscript/func-msgbox.html" target="_blank" rel="noopener noreferrer">VBScript MsgBox 函数<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <h2 id="特殊符号"><a href="#特殊符号" class="header-anchor">#</a> 特殊符号</h2> <ul><li><a href="https://docs.microsoft.com/zh-cn/previous-versions//hh277t8e%28v%3dvs.85%29" target="_blank" rel="noopener noreferrer">String Constants<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <table><thead><tr><th>常数</th> <th>值</th> <th>描述</th></tr></thead> <tbody><tr><td>vbCr</td> <td>Chr(13)</td> <td>回车符 <code>\r</code></td></tr> <tr><td>vbLf</td> <td>Chr(10)</td> <td>换行符 <code>\n</code></td></tr> <tr><td>vbCrLf</td> <td>Chr(13)&amp;Chr(10)</td> <td>回车符与换行符 <code>\r\n</code></td></tr> <tr><td>vbFormFeed</td> <td>Chr(12)</td> <td>换页符；在MicrosoftWindows中不适用。</td></tr> <tr><td>vbNewLine</td> <td>Chr(13)&amp;Chr(10)或Chr(10)</td> <td>平台指定的新行字符；适用于任何平台。</td></tr> <tr><td>vbNullChar</td> <td>Chr(0)</td> <td>值为0的字符。</td></tr> <tr><td>vbNullString</td> <td>值为0的字符串</td> <td>与零长度字符串(&quot;&quot;)不同；用于调用外部过程。</td></tr> <tr><td>vbTab</td> <td>Chr(9)</td> <td>水平附签。</td></tr> <tr><td>vbVerticalTab</td> <td>Chr(11)</td> <td>垂直附签；在MicrosoftWindows中不用</td></tr></tbody></table> <h2 id="函数封装"><a href="#函数封装" class="header-anchor">#</a> 函数封装</h2> <ul><li><a href="https://github.com/eklam/VbsJson" target="_blank" rel="noopener noreferrer">https://github.com/eklam/VbsJson<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <h3 id="自动关闭弹窗"><a href="#自动关闭弹窗" class="header-anchor">#</a> 自动关闭弹窗</h3> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token comment">' TimeOut 单位为秒</span>
<span class="token keyword">Public</span> <span class="token keyword">Sub</span> MsgBoxTimeout<span class="token punctuation">(</span>Text<span class="token punctuation">,</span> Title<span class="token punctuation">,</span> TimeOut<span class="token punctuation">)</span>
    <span class="token keyword">Set</span> WshShell <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span>
    WshShell<span class="token punctuation">.</span>Popup Text<span class="token punctuation">,</span> TimeOut<span class="token punctuation">,</span> Title
<span class="token keyword">End</span> <span class="token keyword">Sub</span>
</code></pre></div><h3 id="数组转换为字符串"><a href="#数组转换为字符串" class="header-anchor">#</a> 数组转换为字符串</h3> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token comment">'数组转换为字符串</span>
<span class="token comment">'Writer         Bajins</span>
<span class="token comment">'Create Date    2019-10-22</span>
<span class="token comment">'arrayName      数组</span>
<span class="token comment">'separator      separator</span>
<span class="token comment">'Example        ConvertArrayToString(array, &quot;,&quot;)</span>
<span class="token keyword">Public</span> <span class="token keyword">Function</span> ConvertArrayToString<span class="token punctuation">(</span>array<span class="token punctuation">,</span> separator<span class="token punctuation">)</span>
    <span class="token keyword">Dim</span> elementString

    <span class="token keyword">For</span> <span class="token keyword">Each</span> element <span class="token keyword">In</span> array
        elementString <span class="token operator">=</span> elementString <span class="token operator">+</span> <span class="token keyword">Cstr</span><span class="token punctuation">(</span>element<span class="token punctuation">)</span> <span class="token operator">+</span> separator
    <span class="token keyword">Next</span>

    elementString <span class="token operator">=</span> StrReverse<span class="token punctuation">(</span>elementString<span class="token punctuation">)</span>
    elementString <span class="token operator">=</span> Replace<span class="token punctuation">(</span>elementString<span class="token punctuation">,</span> separator<span class="token punctuation">,</span><span class="token string">&quot;&quot;</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
    elementString <span class="token operator">=</span> StrReverse<span class="token punctuation">(</span>elementString<span class="token punctuation">)</span>
    <span class="token comment">' 设置返回值</span>
    ConvertArrayToString <span class="token operator">=</span> elementString
<span class="token keyword">End</span> <span class="token keyword">Function</span>
</code></pre></div><h3 id="获取对象的属性和值"><a href="#获取对象的属性和值" class="header-anchor">#</a> 获取对象的属性和值</h3> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token comment">' 获取对象的属性和值</span>
<span class="token comment">'Writer         Bajins</span>
<span class="token comment">'Create Date    2019-10-22</span>
<span class="token comment">'obj            对象</span>
<span class="token comment">'Example        GetObjectPropertieValue(obj)</span>
<span class="token keyword">Public</span> <span class="token keyword">Function</span> GetObjectPropertieValue<span class="token punctuation">(</span>obj<span class="token punctuation">)</span>
    <span class="token keyword">IF</span> <span class="token keyword">Not</span> IsObject<span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token keyword">Then</span>
        <span class="token comment">'Exit Function</span>
        Err<span class="token punctuation">.</span>Raise Err<span class="token punctuation">.</span>Number
    <span class="token keyword">END</span> <span class="token keyword">IF</span>
    
    <span class="token keyword">Dim</span> kv
    
    <span class="token keyword">For</span> <span class="token keyword">Each</span> Propertie <span class="token keyword">in</span> obj<span class="token punctuation">.</span>Properties_
       kv <span class="token operator">=</span> kv <span class="token operator">&amp;</span> Propertie<span class="token punctuation">.</span>name <span class="token operator">&amp;</span> <span class="token string">&quot; : &quot;</span> <span class="token operator">&amp;</span> Propertie<span class="token punctuation">.</span>value <span class="token operator">&amp;</span> vbCrLf
    <span class="token keyword">Next</span>
    
    kv <span class="token operator">=</span> <span class="token string">&quot;属性数量：&quot;</span> <span class="token operator">&amp;</span> obj<span class="token punctuation">.</span>Properties_<span class="token punctuation">.</span>count <span class="token operator">&amp;</span> vbCrLf <span class="token operator">&amp;</span> kv
    
    <span class="token comment">' 设置返回值</span>
    GetObjectPropertieValue <span class="token operator">=</span> kv
<span class="token keyword">End</span> <span class="token keyword">Function</span>
</code></pre></div><h3 id="获取系统信息"><a href="#获取系统信息" class="header-anchor">#</a> 获取系统信息</h3> <blockquote><p>此方式完全不会显示<code>CMD</code>窗口（包括闪现）</p></blockquote> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token comment">' 获取系统位数</span>
<span class="token comment">'Writer         Bajins</span>
<span class="token comment">'Create Date    2019-10-22</span>
<span class="token comment">'Example        GetSystemBit()</span>
<span class="token keyword">Public</span> <span class="token keyword">Function</span> GetSystemBit<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">Set</span> WMIService <span class="token operator">=</span> GetObject<span class="token punctuation">(</span><span class="token string">&quot;winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">Set</span> ComputerSystem <span class="token operator">=</span> WMIService<span class="token punctuation">.</span>InstancesOf<span class="token punctuation">(</span><span class="token string">&quot;Win32_ComputerSystem&quot;</span><span class="token punctuation">)</span>

    <span class="token keyword">For</span> <span class="token keyword">Each</span> System <span class="token keyword">in</span> ComputerSystem
        <span class="token keyword">IF</span> InStr<span class="token punctuation">(</span>System<span class="token punctuation">.</span>SystemType<span class="token punctuation">,</span><span class="token string">&quot;86&quot;</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token keyword">Then</span>
            GetSystemBit <span class="token operator">=</span> <span class="token string">&quot;i386&quot;</span>
            <span class="token keyword">Exit</span> <span class="token keyword">For</span>
        <span class="token keyword">End</span> <span class="token keyword">IF</span>
        
        <span class="token keyword">IF</span> InStr<span class="token punctuation">(</span>System<span class="token punctuation">.</span>SystemType<span class="token punctuation">,</span><span class="token string">&quot;64&quot;</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token keyword">Then</span>
            GetSystemBit <span class="token operator">=</span> <span class="token string">&quot;amd64&quot;</span>
            <span class="token keyword">Exit</span> <span class="token keyword">For</span>
        <span class="token keyword">End</span> <span class="token keyword">IF</span>
    <span class="token keyword">Next</span>

<span class="token keyword">End</span> <span class="token keyword">Function</span>
</code></pre></div><h3 id="隐藏窗口运行"><a href="#隐藏窗口运行" class="header-anchor">#</a> 隐藏窗口运行</h3> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token comment">' 创建运行命令数组</span>
commands <span class="token operator">=</span> Array<span class="token punctuation">(</span><span class="token string">&quot;D:\frp内网穿透工具\frpc.exe -c D:\frp内网穿透工具\frpc.ini&quot;</span><span class="token punctuation">)</span>

<span class="token comment">' 创建运行命令动态数组</span>
<span class="token comment">'Set commands = CreateObject(&quot;System.Collections.ArrayList&quot;)</span>
<span class="token comment">'commands.Add &quot;D:\frp内网穿透工具\frpc.exe -c D:\frp内网穿透工具\frpc.ini&quot;</span>

<span class="token comment">' 启动项键名称</span>
keyName <span class="token operator">=</span> <span class="token string">&quot;frp&quot;</span>

<span class="token keyword">Set</span> shell <span class="token operator">=</span> WScript<span class="token punctuation">.</span>CreateObject<span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">For</span> <span class="token keyword">Each</span> command <span class="token keyword">In</span> commands
    <span class="token comment">' cmd /c运行之后关闭窗口，0隐藏运行，false不同步运行</span>
    shell<span class="token punctuation">.</span>Run <span class="token string">&quot;cmd /c &quot;</span> <span class="token operator">&amp;</span> command<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">false</span>
<span class="token keyword">Next</span>

<span class="token comment">' 注册表项</span>
item <span class="token operator">=</span> <span class="token string">&quot;HKCU\Software\Microsoft\Windows\CurrentVersion\Run\&quot;</span>

<span class="token comment">' 设置开机启动</span>
shell<span class="token punctuation">.</span>RegWrite item <span class="token operator">&amp;</span> keyName<span class="token punctuation">,</span> WScript<span class="token punctuation">.</span>ScriptFullName
</code></pre></div><p><strong>Shell.Application</strong></p> <div class="language-vb extra-class"><pre class="language-vb"><code>CreateObject<span class="token punctuation">(</span><span class="token string">&quot;Shell.Application&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>ShellExecute <span class="token string">&quot;demo.exe&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;c:/&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span>
</code></pre></div><h3 id="查看进程是否存在"><a href="#查看进程是否存在" class="header-anchor">#</a> 查看进程是否存在</h3> <div class="language-vb extra-class"><pre class="language-vb"><code>ProcesseName<span class="token operator">=</span><span class="token string">&quot;rclone.exe&quot;</span>

<span class="token comment">' 查找进程</span>
<span class="token keyword">Set</span> WMIService <span class="token operator">=</span> GetObject<span class="token punctuation">(</span><span class="token string">&quot;winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">Set</span> Processes <span class="token operator">=</span> WMIService<span class="token punctuation">.</span>ExecQuery<span class="token punctuation">(</span><span class="token string">&quot;select * from win32_process where name='&quot;</span> <span class="token operator">&amp;</span> ProcesseName <span class="token operator">&amp;</span> <span class="token string">&quot;'&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">For</span> <span class="token keyword">Each</span> Process <span class="token keyword">In</span> Processes
    <span class="token comment">' 比较两个字符串</span>
    <span class="token keyword">If</span> InStr<span class="token punctuation">(</span>UCase<span class="token punctuation">(</span>Process<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">,</span> UCase<span class="token punctuation">(</span>ProcesseName<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">0</span> <span class="token keyword">Then</span>
        <span class="token comment">' 如果进程存在就不重复执行后面的代码</span>
        <span class="token keyword">Exit</span> <span class="token keyword">for</span>
    <span class="token keyword">End</span> <span class="token keyword">If</span>
    <span class="token comment">' 运行程序</span>
    <span class="token keyword">Set</span> WS <span class="token operator">=</span> Wscript<span class="token punctuation">.</span>CreateObject<span class="token punctuation">(</span><span class="token string">&quot;Wscript.Shell&quot;</span><span class="token punctuation">)</span>
    WS<span class="token punctuation">.</span>Run <span class="token string">&quot;rclone mount GDrive:/ x: --cache-dir F:\Temp --vfs-cache-mode writes&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span>
<span class="token keyword">Next</span>
</code></pre></div><h3 id="ping"><a href="#ping" class="header-anchor">#</a> Ping</h3> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token keyword">Function</span> Ping<span class="token punctuation">(</span>strHostName<span class="token punctuation">)</span> 
  <span class="token keyword">Dim</span> colPingResults<span class="token punctuation">,</span> objPingResult<span class="token punctuation">,</span> strQuery 
  <span class="token comment">' 定义WMI查询</span>
  strQuery <span class="token operator">=</span> <span class="token string">&quot;SELECT * FROM Win32_PingStatus WHERE Address = '&quot;</span> <span class="token operator">&amp;</span> strHostName <span class="token operator">&amp;</span> <span class="token string">&quot;'&quot;</span> 
  <span class="token comment">' 运行WMI查询</span>
  <span class="token comment">' GetObject(&quot;winmgmts://./root/cimv2&quot;)</span>
  <span class="token keyword">Set</span> colPingResults <span class="token operator">=</span> GetObject<span class="token punctuation">(</span><span class="token string">&quot;winmgmts:root\cimv2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>ExecQuery<span class="token punctuation">(</span>strQuery<span class="token punctuation">)</span> 
  <span class="token comment">' 将查询结果转换为True或False</span>
  <span class="token keyword">For</span> <span class="token keyword">Each</span> objPingResult <span class="token keyword">In</span> colPingResults 
    <span class="token keyword">If</span> <span class="token keyword">Not</span> IsObject<span class="token punctuation">(</span>objPingResult<span class="token punctuation">)</span> <span class="token keyword">Then</span> 
      Ping <span class="token operator">=</span> <span class="token boolean">False</span> 
    <span class="token keyword">Else</span> 
      <span class="token keyword">If</span> objPingResult<span class="token punctuation">.</span>StatusCode <span class="token operator">=</span> <span class="token number">0</span> <span class="token keyword">Then</span> 
        Ping <span class="token operator">=</span> <span class="token boolean">True</span> 
      <span class="token keyword">Else</span> 
        Ping <span class="token operator">=</span> <span class="token boolean">False</span> 
      <span class="token keyword">End</span> <span class="token keyword">If</span> 
      <span class="token comment">'WScript.Echo &quot;Ping status code for &quot; &amp; strHostName &amp; &quot;: &quot; &amp; objPingResult.StatusCode </span>
    <span class="token keyword">End</span> <span class="token keyword">If</span> 
  <span class="token keyword">Next</span> 
  <span class="token keyword">Set</span> colPingResults <span class="token operator">=</span> <span class="token boolean">Nothing</span> 
<span class="token keyword">End</span> <span class="token keyword">Function</span>
</code></pre></div><h3 id="监视网络连接"><a href="#监视网络连接" class="header-anchor">#</a> 监视网络连接</h3> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token keyword">Set</span> objWMIService <span class="token operator">=</span> GetObject<span class="token punctuation">(</span><span class="token string">&quot;winmgmts:\\.\root\wmi&quot;</span><span class="token punctuation">)</span>
<span class="token comment">' 执行事件订阅查询以接收事件。事件订阅查询定义了要监视的托管环境的更改。发生更改时，WMI基础结构会将事件描述为调用脚本。</span>
<span class="token keyword">Set</span> colMonitoredEvents <span class="token operator">=</span> objWMIService<span class="token punctuation">.</span>ExecNotificationQuery<span class="token punctuation">(</span><span class="token string">&quot;Select * from MSNdis_StatusMediaConnect&quot;</span><span class="token punctuation">)</span> 
<span class="token keyword">Do</span> <span class="token keyword">While</span> <span class="token boolean">True</span> 
    <span class="token keyword">Set</span> strLatestEvent <span class="token operator">=</span> colMonitoredEvents<span class="token punctuation">.</span>NextEvent 
    Wscript<span class="token punctuation">.</span>Echo <span class="token string">&quot;已建立网络连接：&quot;</span>
    WScript<span class="token punctuation">.</span>Echo strLatestEvent<span class="token punctuation">.</span>InstanceName<span class="token punctuation">,</span> Now
    Wscript<span class="token punctuation">.</span>Echo 
<span class="token keyword">Loop</span>
</code></pre></div><h3 id="监视网络断开"><a href="#监视网络断开" class="header-anchor">#</a> 监视网络断开</h3> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token keyword">Set</span> objWMIService <span class="token operator">=</span> GetObject<span class="token punctuation">(</span><span class="token string">&quot;winmgmts:\\.\root\wmi&quot;</span><span class="token punctuation">)</span>
<span class="token comment">' 执行事件订阅查询以接收事件。事件订阅查询定义了要监视的托管环境的更改。发生更改时，WMI基础结构会将事件描述为调用脚本。</span>
<span class="token keyword">Set</span> colMonitoredEvents <span class="token operator">=</span> objWMIService<span class="token punctuation">.</span>ExecNotificationQuery<span class="token punctuation">(</span><span class="token string">&quot;Select * from MSNdis_StatusMediaDisconnect&quot;</span><span class="token punctuation">)</span> 
<span class="token keyword">Do</span> <span class="token keyword">While</span> <span class="token boolean">True</span> 
    <span class="token keyword">Set</span> strLatestEvent <span class="token operator">=</span> colMonitoredEvents<span class="token punctuation">.</span>NextEvent 
    Wscript<span class="token punctuation">.</span>Echo <span class="token string">&quot;网络连接已丢失：&quot;</span>
    WScript<span class="token punctuation">.</span>Echo strLatestEvent<span class="token punctuation">.</span>InstanceName<span class="token punctuation">,</span> Now
<span class="token keyword">Loop</span>
</code></pre></div><h3 id="设置壁纸"><a href="#设置壁纸" class="header-anchor">#</a> 设置壁纸</h3> <blockquote><p>使用API触发图片文件右键菜单上的 <code>设置为桌面背景(B)</code></p></blockquote> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token keyword">Set</span> shApp <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;Shell.Application&quot;</span><span class="token punctuation">)</span>
<span class="token comment">' 获取文件</span>
<span class="token keyword">Set</span> picFile <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;Scripting.FileSystemObject&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>GetFile<span class="token punctuation">(</span><span class="token string">&quot;C:\Users\bajin\Desktop\CachedImage_1920_1080_POS4.jpg&quot;</span><span class="token punctuation">)</span>
<span class="token comment">' 获取文件上的所有右键菜单项</span>
<span class="token comment">' Set items = shApp.NameSpace(picFile.ParentFolder.Path).ParseName(picFile.Name).Verbs()</span>
<span class="token keyword">Set</span> items <span class="token operator">=</span> shApp<span class="token punctuation">.</span><span class="token keyword">NameSpace</span><span class="token punctuation">(</span>picFile<span class="token punctuation">.</span>ParentFolder<span class="token punctuation">.</span>Path<span class="token punctuation">)</span><span class="token punctuation">.</span>Items<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Item<span class="token punctuation">(</span>picFile<span class="token punctuation">.</span>Name<span class="token punctuation">)</span><span class="token punctuation">.</span>Verbs<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">' 遍历所有菜单项</span>
<span class="token comment">' For i=0 To items.Count - 1</span>
<span class="token comment">' Set item = items.Item(i)</span>
<span class="token keyword">For</span> <span class="token keyword">Each</span> item <span class="token keyword">In</span> items
    <span class="token comment">' 注意执行的脚本文件需要为简体中文编码</span>
    <span class="token keyword">If</span> item<span class="token punctuation">.</span>Name <span class="token operator">=</span> <span class="token string">&quot;设置为桌面背景(&amp;B)&quot;</span> <span class="token keyword">Then</span>
    <span class="token comment">' If strcomp(item.Name,&quot;设置为桌面背景(&amp;B)&quot;) = 0 Then</span>
        item<span class="token punctuation">.</span>DoIt
    <span class="token keyword">END</span> <span class="token keyword">IF</span>
<span class="token keyword">Next</span>
</code></pre></div><h3 id="刷新桌面"><a href="#刷新桌面" class="header-anchor">#</a> 刷新桌面</h3> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token comment">' 切换到桌面</span>
CreateObject<span class="token punctuation">(</span><span class="token string">&quot;Shell.Application&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>ToggleDesktop<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">' 刷新桌面</span>
CreateObject<span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>SendKeys<span class="token punctuation">(</span><span class="token string">&quot;{F5}&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">Set</span> WSHShell <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span>
<span class="token comment">' 切换到桌面</span>
<span class="token comment">'WSHShell.AppActivate(&quot;Program Manager&quot;)</span>
WSHShell<span class="token punctuation">.</span>AppActivate<span class="token punctuation">(</span>WSHShell<span class="token punctuation">.</span>SpecialFolders<span class="token punctuation">(</span><span class="token string">&quot;Desktop&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">' 刷新桌面</span>
WSHShell<span class="token punctuation">.</span>SendKeys<span class="token punctuation">(</span><span class="token string">&quot;{F5}&quot;</span><span class="token punctuation">)</span>

<span class="token comment">' 下面这两种方式没看出效果</span>
CreateObject<span class="token punctuation">(</span><span class="token string">&quot;shell.application&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token keyword">Namespace</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Self<span class="token punctuation">.</span>invokeVerb<span class="token punctuation">(</span><span class="token string">&quot;R&amp;efresh&quot;</span><span class="token punctuation">)</span>
CreateObject<span class="token punctuation">(</span><span class="token string">&quot;shell.application&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token keyword">Namespace</span><span class="token punctuation">(</span><span class="token number">&amp;H10</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Self<span class="token punctuation">.</span>invokeVerb<span class="token punctuation">(</span><span class="token string">&quot;Refresh&quot;</span><span class="token punctuation">)</span>

<span class="token comment">' 刷新桌面、任务栏、OSD（相当于重启资源管理器）</span>
<span class="token keyword">Set</span> WSHShell <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span>
WSHShell<span class="token punctuation">.</span>Run <span class="token string">&quot;regsvr32.exe /s /n /i:/UserInstall %SystemRoot%\system32\themeui.dll&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">True</span>

<span class="token comment">' 效果不太好，有时刷新成功，有时失败</span>
<span class="token keyword">Set</span> WSHShell <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span>
WSHShell<span class="token punctuation">.</span>Run <span class="token string">&quot;RunDll32 USER32,UpdatePerUserSystemParameters&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">True</span>

<span class="token comment">' assoc文件关联时会自动刷新桌面，可能报错</span>
<span class="token keyword">Set</span> WSHShell <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span>
WSHShell<span class="token punctuation">.</span>Run <span class="token string">&quot;assoc .=.&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">True</span>

<span class="token comment">' 重启资源管理器并恢复打开的目录，暂时不可用</span>
<span class="token keyword">Function</span> RestartExplorer<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">Dim</span> arrURL<span class="token punctuation">(</span><span class="token punctuation">)</span>
    n <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span>
    <span class="token keyword">Set</span> shApp <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;Shell.Application&quot;</span><span class="token punctuation">)</span>
    <span class="token comment">' 遍历所有打开的窗口</span>
    <span class="token keyword">For</span> <span class="token keyword">Each</span> oWin <span class="token keyword">In</span> shApp<span class="token punctuation">.</span>Windows
        <span class="token comment">' 如果打开的窗口为资源管理器</span>
        <span class="token keyword">If</span> Instr<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> oWin<span class="token punctuation">.</span>FullName<span class="token punctuation">,</span> <span class="token string">&quot;\explorer.exe&quot;</span><span class="token punctuation">,</span> vbTextCompare<span class="token punctuation">)</span> <span class="token keyword">Then</span>
            n <span class="token operator">=</span> n <span class="token operator">+</span> <span class="token number">1</span>
            <span class="token keyword">ReDim</span> Preserve arrURL<span class="token punctuation">(</span>n<span class="token punctuation">)</span>
            arrURL<span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token operator">=</span> oWin<span class="token punctuation">.</span>LocationURL
            <span class="token comment">'oWin.Document.folder.title</span>
            <span class="token comment">' 关闭当前打开的文件夹</span>
            <span class="token comment">'oWin.quit</span>
        <span class="token keyword">End</span> <span class="token keyword">If</span>
    <span class="token keyword">Next</span>
    <span class="token comment">' 结束资源管理器进程</span>
    CreateObject<span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Run <span class="token string">&quot;taskkill /f /im explorer.exe &gt;nul 2&gt;nul&amp;start explorer.exe&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">True</span>
    <span class="token comment">' 遍历并打开之前的窗口</span>
    <span class="token keyword">For</span> <span class="token keyword">Each</span> strURL <span class="token keyword">In</span> arrURL
        <span class="token comment">'shApp.Open strURL</span>
        shApp<span class="token punctuation">.</span>Explore strURL
    <span class="token keyword">Next</span>
<span class="token keyword">End</span> <span class="token keyword">Function</span>
</code></pre></div><h3 id="字符编码转换"><a href="#字符编码转换" class="header-anchor">#</a> 字符编码转换</h3> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token comment">' 将UTF8编码文字转换为GB编码文字</span>
<span class="token keyword">function</span> UTF2GB<span class="token punctuation">(</span>UTFStr<span class="token punctuation">)</span>
    <span class="token keyword">for</span> Dig<span class="token operator">=</span><span class="token number">1</span> <span class="token keyword">to</span> len<span class="token punctuation">(</span>UTFStr<span class="token punctuation">)</span>
    <span class="token comment">'如果UTF8编码文字以%开头则进行转换</span>
    <span class="token keyword">if</span> mid<span class="token punctuation">(</span>UTFStr<span class="token punctuation">,</span>Dig<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token string">&quot;%&quot;</span> <span class="token keyword">then</span>
        <span class="token comment">'UTF8编码文字大于8则转换为汉字</span>
        <span class="token keyword">if</span> len<span class="token punctuation">(</span>UTFStr<span class="token punctuation">)</span> <span class="token operator">&gt;</span><span class="token operator">=</span> Dig<span class="token operator">+</span><span class="token number">8</span> <span class="token keyword">then</span>
            GBStr<span class="token operator">=</span>GBStr <span class="token operator">&amp;</span> ConvChinese<span class="token punctuation">(</span>mid<span class="token punctuation">(</span>UTFStr<span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            Dig<span class="token operator">=</span>Dig<span class="token operator">+</span><span class="token number">8</span>
        <span class="token keyword">else</span>
            GBStr<span class="token operator">=</span>GBStr <span class="token operator">&amp;</span> mid<span class="token punctuation">(</span>UTFStr<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token keyword">end</span> <span class="token keyword">if</span>
    <span class="token keyword">else</span>
        GBStr<span class="token operator">=</span>GBStr <span class="token operator">&amp;</span> mid<span class="token punctuation">(</span>UTFStr<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">end</span> <span class="token keyword">if</span>
    <span class="token keyword">next</span>
    UTF2GB<span class="token operator">=</span>GBStr
<span class="token keyword">end</span> <span class="token keyword">function</span>

<span class="token comment">' UTF8编码文字将转换为汉字</span>
<span class="token keyword">function</span> ConvChinese<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
     A<span class="token operator">=</span>split<span class="token punctuation">(</span>mid<span class="token punctuation">(</span>x<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">&quot;%&quot;</span><span class="token punctuation">)</span>
     i<span class="token operator">=</span><span class="token number">0</span>
     j<span class="token operator">=</span><span class="token number">0</span>
  <span class="token keyword">for</span> i<span class="token operator">=</span><span class="token number">0</span> <span class="token keyword">to</span> ubound<span class="token punctuation">(</span>A<span class="token punctuation">)</span>
       A<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token operator">=</span>c16to2<span class="token punctuation">(</span>A<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token keyword">next</span>
  <span class="token keyword">for</span> i<span class="token operator">=</span><span class="token number">0</span> <span class="token keyword">to</span> ubound<span class="token punctuation">(</span>A<span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span>
      DigS<span class="token operator">=</span>instr<span class="token punctuation">(</span>A<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">&quot;0&quot;</span><span class="token punctuation">)</span>
      Unicode<span class="token operator">=</span><span class="token string">&quot;&quot;</span>
    <span class="token keyword">for</span> j<span class="token operator">=</span><span class="token number">1</span> <span class="token keyword">to</span> DigS<span class="token operator">-</span><span class="token number">1</span>
      <span class="token keyword">if</span> j<span class="token operator">=</span><span class="token number">1</span> <span class="token keyword">then</span>
          A<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token operator">=</span>right<span class="token punctuation">(</span>A<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">,</span>len<span class="token punctuation">(</span>A<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-</span>DigS<span class="token punctuation">)</span>
          Unicode<span class="token operator">=</span>Unicode <span class="token operator">&amp;</span> A<span class="token punctuation">(</span>i<span class="token punctuation">)</span>
      <span class="token keyword">else</span>
           i<span class="token operator">=</span>i<span class="token operator">+</span><span class="token number">1</span>
           A<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token operator">=</span>right<span class="token punctuation">(</span>A<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">,</span>len<span class="token punctuation">(</span>A<span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span>
           Unicode<span class="token operator">=</span>Unicode <span class="token operator">&amp;</span> A<span class="token punctuation">(</span>i<span class="token punctuation">)</span>
      <span class="token keyword">end</span> <span class="token keyword">if</span>
    <span class="token keyword">next</span>
 
    <span class="token keyword">if</span> len<span class="token punctuation">(</span>c2to16<span class="token punctuation">(</span>Unicode<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">4</span> <span class="token keyword">then</span>
         ConvChinese<span class="token operator">=</span>ConvChinese <span class="token operator">&amp;</span> chrw<span class="token punctuation">(</span>int<span class="token punctuation">(</span><span class="token string">&quot;&amp;H&quot;</span> <span class="token operator">&amp;</span> c2to16<span class="token punctuation">(</span>Unicode<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">else</span>
         ConvChinese<span class="token operator">=</span>ConvChinese <span class="token operator">&amp;</span> chr<span class="token punctuation">(</span>int<span class="token punctuation">(</span><span class="token string">&quot;&amp;H&quot;</span> <span class="token operator">&amp;</span> c2to16<span class="token punctuation">(</span>Unicode<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">end</span> <span class="token keyword">if</span>
  <span class="token keyword">next</span>
<span class="token keyword">end</span> <span class="token keyword">function</span>
 
<span class="token comment">'二进制代码转换为十六进制代码</span>
<span class="token keyword">function</span> c2to16<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
    i<span class="token operator">=</span><span class="token number">1</span>
    <span class="token keyword">for</span> i<span class="token operator">=</span><span class="token number">1</span> <span class="token keyword">to</span> len<span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token keyword">step</span> <span class="token number">4</span>
            c2to16<span class="token operator">=</span>c2to16 <span class="token operator">&amp;</span> hex<span class="token punctuation">(</span>c2to10<span class="token punctuation">(</span>mid<span class="token punctuation">(</span>x<span class="token punctuation">,</span>i<span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">next</span>
<span class="token keyword">end</span> <span class="token keyword">function</span>
<span class="token comment">'二进制代码转换为十进制代码</span>
<span class="token keyword">function</span> c2to10<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
    c2to10<span class="token operator">=</span><span class="token number">0</span>
    <span class="token keyword">if</span> x<span class="token operator">=</span><span class="token string">&quot;0&quot;</span> <span class="token keyword">then</span> <span class="token keyword">exit</span> <span class="token keyword">function</span>
        i<span class="token operator">=</span><span class="token number">0</span>
    <span class="token keyword">for</span> i<span class="token operator">=</span> <span class="token number">0</span> <span class="token keyword">to</span> len<span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token number">1</span>
        <span class="token keyword">if</span> mid<span class="token punctuation">(</span>x<span class="token punctuation">,</span>len<span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token operator">-</span>i<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token string">&quot;1&quot;</span> <span class="token keyword">then</span> c2to10<span class="token operator">=</span>c2to10<span class="token operator">+</span><span class="token number">2</span><span class="token operator">^</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
    <span class="token keyword">next</span>
<span class="token keyword">end</span> <span class="token keyword">function</span>
 
<span class="token comment">'十六进制代码转换为二进制代码</span>
<span class="token keyword">function</span> c16to2<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
    i<span class="token operator">=</span><span class="token number">0</span>
    <span class="token keyword">for</span> i<span class="token operator">=</span><span class="token number">1</span> <span class="token keyword">to</span> len<span class="token punctuation">(</span>trim<span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span>
        tempstr<span class="token operator">=</span> c10to2<span class="token punctuation">(</span><span class="token keyword">cint</span><span class="token punctuation">(</span>int<span class="token punctuation">(</span><span class="token string">&quot;&amp;h&quot;</span> <span class="token operator">&amp;</span> mid<span class="token punctuation">(</span>x<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">do</span> <span class="token keyword">while</span> len<span class="token punctuation">(</span>tempstr<span class="token punctuation">)</span><span class="token operator">&lt;</span><span class="token number">4</span>
            tempstr<span class="token operator">=</span><span class="token string">&quot;0&quot;</span> <span class="token operator">&amp;</span> tempstr
        <span class="token keyword">loop</span>
        c16to2<span class="token operator">=</span>c16to2 <span class="token operator">&amp;</span> tempstr
    <span class="token keyword">next</span>
<span class="token keyword">end</span> <span class="token keyword">function</span>
 
<span class="token comment">'十进制代码转换为二进制代码</span>
<span class="token keyword">function</span> c10to2<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
    mysign<span class="token operator">=</span>sgn<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
    x<span class="token operator">=</span>abs<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
    DigS<span class="token operator">=</span><span class="token number">1</span>
    <span class="token keyword">do</span>
        <span class="token keyword">if</span> x<span class="token operator">&lt;</span><span class="token number">2</span><span class="token operator">^</span>DigS <span class="token keyword">then</span>
        <span class="token keyword">exit</span> <span class="token keyword">do</span>
        <span class="token keyword">else</span>
            DigS<span class="token operator">=</span>DigS<span class="token operator">+</span><span class="token number">1</span>
        <span class="token keyword">end</span> <span class="token keyword">if</span>
    <span class="token keyword">loop</span>
        tempnum<span class="token operator">=</span>x

        i<span class="token operator">=</span><span class="token number">0</span>
    <span class="token keyword">for</span> i<span class="token operator">=</span>DigS <span class="token keyword">to</span> <span class="token number">1</span> <span class="token keyword">step</span><span class="token operator">-</span><span class="token number">1</span>
        <span class="token keyword">if</span> tempnum<span class="token operator">&gt;</span><span class="token operator">=</span><span class="token number">2</span><span class="token operator">^</span><span class="token punctuation">(</span>i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">then</span>
            tempnum<span class="token operator">=</span>tempnum<span class="token operator">-</span><span class="token number">2</span><span class="token operator">^</span><span class="token punctuation">(</span>i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
            c10to2<span class="token operator">=</span>c10to2 <span class="token operator">&amp;</span> <span class="token string">&quot;1&quot;</span>
        <span class="token keyword">else</span>
            c10to2<span class="token operator">=</span>c10to2 <span class="token operator">&amp;</span> <span class="token string">&quot;0&quot;</span>
        <span class="token keyword">end</span> <span class="token keyword">if</span>
    <span class="token keyword">next</span>
    <span class="token keyword">if</span> mysign<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span> <span class="token keyword">then</span> c10to2<span class="token operator">=</span><span class="token string">&quot;-&quot;</span> <span class="token operator">&amp;</span> c10to2
<span class="token keyword">end</span> <span class="token keyword">function</span>
 
<span class="token comment">'GB转unicode---将GB编码文字转换为unicode编码文字</span>
<span class="token keyword">function</span> chinese2unicode<span class="token punctuation">(</span>Str<span class="token punctuation">)</span>
  <span class="token keyword">dim</span> i
  <span class="token keyword">dim</span> Str_one
  <span class="token keyword">dim</span> Str_unicode
  <span class="token keyword">if</span><span class="token punctuation">(</span>isnull<span class="token punctuation">(</span>Str<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">then</span>
     <span class="token keyword">exit</span> <span class="token keyword">function</span>
  <span class="token keyword">end</span> <span class="token keyword">if</span>
  <span class="token keyword">for</span> i<span class="token operator">=</span><span class="token number">1</span> <span class="token keyword">to</span> len<span class="token punctuation">(</span>Str<span class="token punctuation">)</span>
     Str_one<span class="token operator">=</span>Mid<span class="token punctuation">(</span>Str<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
     Str_unicode<span class="token operator">=</span>Str_unicode<span class="token operator">&amp;</span>chr<span class="token punctuation">(</span><span class="token number">38</span><span class="token punctuation">)</span>
     Str_unicode<span class="token operator">=</span>Str_unicode<span class="token operator">&amp;</span>chr<span class="token punctuation">(</span><span class="token number">35</span><span class="token punctuation">)</span>
     Str_unicode<span class="token operator">=</span>Str_unicode<span class="token operator">&amp;</span>chr<span class="token punctuation">(</span><span class="token number">120</span><span class="token punctuation">)</span>
     Str_unicode<span class="token operator">=</span>Str_unicode<span class="token operator">&amp;</span> Hex<span class="token punctuation">(</span>ascw<span class="token punctuation">(</span>Str_one<span class="token punctuation">)</span><span class="token punctuation">)</span>
     Str_unicode<span class="token operator">=</span>Str_unicode<span class="token operator">&amp;</span>chr<span class="token punctuation">(</span><span class="token number">59</span><span class="token punctuation">)</span>
  <span class="token keyword">next</span>
   chinese2unicode<span class="token operator">=</span>Str_unicode
<span class="token keyword">end</span> <span class="token keyword">function</span>   
 
<span class="token comment">'URL解码</span>
<span class="token keyword">Function</span> URLDecode<span class="token punctuation">(</span>enStr<span class="token punctuation">)</span>
<span class="token keyword">dim</span> deStr
<span class="token keyword">dim</span> c<span class="token punctuation">,</span>v
deStr<span class="token operator">=</span><span class="token string">&quot;&quot;</span>
<span class="token keyword">for</span> i<span class="token operator">=</span><span class="token number">1</span> <span class="token keyword">to</span> len<span class="token punctuation">(</span>enStr<span class="token punctuation">)</span>
    c<span class="token operator">=</span>Mid<span class="token punctuation">(</span>enStr<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> c<span class="token operator">=</span><span class="token string">&quot;%&quot;</span> <span class="token keyword">then</span>
    v<span class="token operator">=</span>eval<span class="token punctuation">(</span><span class="token string">&quot;&amp;h&quot;</span><span class="token operator">+</span>Mid<span class="token punctuation">(</span>enStr<span class="token punctuation">,</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> v<span class="token operator">&lt;</span><span class="token number">128</span> <span class="token keyword">then</span>
        deStr<span class="token operator">=</span>deStr<span class="token operator">&amp;</span>chr<span class="token punctuation">(</span>v<span class="token punctuation">)</span>
        i<span class="token operator">=</span>i<span class="token operator">+</span><span class="token number">2</span>
    <span class="token keyword">else</span>
    <span class="token keyword">if</span> isvalidhex<span class="token punctuation">(</span>mid<span class="token punctuation">(</span>enstr<span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">then</span>
        <span class="token keyword">if</span> isvalidhex<span class="token punctuation">(</span>mid<span class="token punctuation">(</span>enstr<span class="token punctuation">,</span>i<span class="token operator">+</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">then</span>
        v<span class="token operator">=</span>eval<span class="token punctuation">(</span><span class="token string">&quot;&amp;h&quot;</span><span class="token operator">+</span>Mid<span class="token punctuation">(</span>enStr<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">+</span>Mid<span class="token punctuation">(</span>enStr<span class="token punctuation">,</span>i<span class="token operator">+</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        deStr<span class="token operator">=</span>deStr<span class="token operator">&amp;</span>chr<span class="token punctuation">(</span>v<span class="token punctuation">)</span>
        i<span class="token operator">=</span>i<span class="token operator">+</span><span class="token number">5</span>
        <span class="token keyword">else</span>
        v<span class="token operator">=</span>eval<span class="token punctuation">(</span><span class="token string">&quot;&amp;h&quot;</span><span class="token operator">+</span>Mid<span class="token punctuation">(</span>enStr<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token keyword">cstr</span><span class="token punctuation">(</span>hex<span class="token punctuation">(</span>asc<span class="token punctuation">(</span>Mid<span class="token punctuation">(</span>enStr<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        deStr<span class="token operator">=</span>deStr<span class="token operator">&amp;</span>chr<span class="token punctuation">(</span>v<span class="token punctuation">)</span>
        i<span class="token operator">=</span>i<span class="token operator">+</span><span class="token number">3</span>
        <span class="token keyword">end</span> <span class="token keyword">if</span>
    <span class="token keyword">else</span>
        destr<span class="token operator">=</span>destr<span class="token operator">&amp;</span>c
    <span class="token keyword">end</span> <span class="token keyword">if</span>
    <span class="token keyword">end</span> <span class="token keyword">if</span>
    <span class="token keyword">else</span>
    <span class="token keyword">if</span> c<span class="token operator">=</span><span class="token string">&quot;+&quot;</span> <span class="token keyword">then</span>
        deStr<span class="token operator">=</span>deStr<span class="token operator">&amp;</span><span class="token string">&quot; &quot;</span>
    <span class="token keyword">else</span>
        deStr<span class="token operator">=</span>deStr<span class="token operator">&amp;</span>c
    <span class="token keyword">end</span> <span class="token keyword">if</span>
    <span class="token keyword">end</span> <span class="token keyword">if</span>
<span class="token keyword">next</span>
URLDecode<span class="token operator">=</span>deStr
<span class="token keyword">end</span> <span class="token keyword">function</span>
 
<span class="token comment">'判断是否为有效的十六进制代码</span>
<span class="token keyword">function</span> isvalidhex<span class="token punctuation">(</span>str<span class="token punctuation">)</span>
    <span class="token keyword">dim</span> c
    isvalidhex<span class="token operator">=</span><span class="token boolean">true</span>
    str<span class="token operator">=</span>ucase<span class="token punctuation">(</span>str<span class="token punctuation">)</span>
    <span class="token keyword">if</span> len<span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token operator">&lt;</span><span class="token operator">&gt;</span><span class="token number">3</span> <span class="token keyword">then</span> isvalidhex<span class="token operator">=</span><span class="token boolean">false</span><span class="token punctuation">:</span><span class="token keyword">exit</span> <span class="token keyword">function</span>
    <span class="token keyword">if</span> left<span class="token punctuation">(</span>str<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">&lt;</span><span class="token operator">&gt;</span><span class="token string">&quot;%&quot;</span> <span class="token keyword">then</span> isvalidhex<span class="token operator">=</span><span class="token boolean">false</span><span class="token punctuation">:</span><span class="token keyword">exit</span> <span class="token keyword">function</span>
    c<span class="token operator">=</span>mid<span class="token punctuation">(</span>str<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token keyword">not</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>c<span class="token operator">&gt;</span><span class="token operator">=</span><span class="token string">&quot;0&quot;</span><span class="token punctuation">)</span> <span class="token keyword">and</span> <span class="token punctuation">(</span>c<span class="token operator">&lt;</span><span class="token operator">=</span><span class="token string">&quot;9&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">or</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>c<span class="token operator">&gt;</span><span class="token operator">=</span><span class="token string">&quot;A&quot;</span><span class="token punctuation">)</span> <span class="token keyword">and</span> <span class="token punctuation">(</span>c<span class="token operator">&lt;</span><span class="token operator">=</span><span class="token string">&quot;Z&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">then</span> isvalidhex<span class="token operator">=</span><span class="token boolean">false</span><span class="token punctuation">:</span><span class="token keyword">exit</span> <span class="token keyword">function</span>
    c<span class="token operator">=</span>mid<span class="token punctuation">(</span>str<span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token keyword">not</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">(</span>c<span class="token operator">&gt;</span><span class="token operator">=</span><span class="token string">&quot;0&quot;</span><span class="token punctuation">)</span> <span class="token keyword">and</span> <span class="token punctuation">(</span>c<span class="token operator">&lt;</span><span class="token operator">=</span><span class="token string">&quot;9&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">or</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>c<span class="token operator">&gt;</span><span class="token operator">=</span><span class="token string">&quot;A&quot;</span><span class="token punctuation">)</span> <span class="token keyword">and</span> <span class="token punctuation">(</span>c<span class="token operator">&lt;</span><span class="token operator">=</span><span class="token string">&quot;Z&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">then</span> isvalidhex<span class="token operator">=</span><span class="token boolean">false</span><span class="token punctuation">:</span><span class="token keyword">exit</span> <span class="token keyword">function</span>
<span class="token keyword">end</span> <span class="token keyword">function</span>
</code></pre></div><h3 id="选择文件对话框"><a href="#选择文件对话框" class="header-anchor">#</a> 选择文件对话框</h3> <ul><li><a href="https://www.cnblogs.com/bitssea/p/12684322.html" target="_blank" rel="noopener noreferrer">VBScript - 弹出“文件选择对话框”方法大全！<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token comment">' GetStandardStream获取TextStream对象.参数：0输入流,1输出流,2错误流.</span>
hta<span class="token operator">=</span><span class="token string">&quot;&quot;&quot;about:&lt;input type=file id=f&gt;&lt;script&gt;f.click();&quot;</span> <span class="token operator">&amp;</span> <span class="token operator">_</span>
    <span class="token string">&quot;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(f.value);&quot;</span> <span class="token operator">&amp;</span> <span class="token operator">_</span>
    <span class="token string">&quot;close();resizeTo(0,0);&lt;/script&gt;&quot;&quot;&quot;</span>
<span class="token comment">' 打开对话框</span>
<span class="token keyword">Set</span> oExec <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Exec<span class="token punctuation">(</span><span class="token string">&quot;mshta.exe &quot;</span> <span class="token operator">&amp;</span> hta<span class="token punctuation">)</span>
<span class="token comment">' 输出选择的，文件的路径</span>
MsgBox oExec<span class="token punctuation">.</span>StdOut<span class="token punctuation">.</span>ReadLine

<span class="token comment">' GetStandardStream获取TextStream对象.参数：0输入流,1输出流,2错误流.</span>
hta<span class="token operator">=</span><span class="token string">&quot;&quot;&quot;&lt;input type=file id=f&gt;&lt;script&gt;f.click();&quot;</span> <span class="token operator">&amp;</span> <span class="token operator">_</span>
    <span class="token string">&quot;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(f.value);&quot;</span> <span class="token operator">&amp;</span> <span class="token operator">_</span>
    <span class="token string">&quot;[close()];resizeTo(0,0);&lt;/script&gt;&quot;&quot;&quot;</span>
<span class="token comment">' 打开对话框</span>
<span class="token keyword">Set</span> oExec <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Exec<span class="token punctuation">(</span><span class="token string">&quot;mshta vbscript:&quot;</span> <span class="token operator">&amp;</span> hta<span class="token punctuation">)</span>
<span class="token comment">' 输出选择的，文件的路径</span>
MsgBox oExec<span class="token punctuation">.</span>StdOut<span class="token punctuation">.</span>ReadAll


<span class="token keyword">Function</span> BrowseForFile<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">Dim</span> shell <span class="token punctuation">:</span> <span class="token keyword">Set</span> shell <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;Shell.Application&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">Dim</span> file <span class="token punctuation">:</span> <span class="token keyword">Set</span> file <span class="token operator">=</span> shell<span class="token punctuation">.</span>BrowseForFolder<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">&quot;Choose a file:&quot;</span><span class="token punctuation">,</span> <span class="token number">&amp;H4000</span><span class="token punctuation">,</span> <span class="token string">&quot;C:\&quot;</span><span class="token punctuation">)</span>
    BrowseForFile <span class="token operator">=</span> file<span class="token punctuation">.</span>self<span class="token punctuation">.</span>Path
<span class="token keyword">End</span> <span class="token keyword">Function</span>

<span class="token keyword">Function</span> BrowseForFile<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">With</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span>
        <span class="token keyword">Dim</span> fso <span class="token punctuation">:</span> <span class="token keyword">Set</span> fso <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;Scripting.FileSystemObject&quot;</span><span class="token punctuation">)</span>
        <span class="token keyword">Dim</span> tempFolder <span class="token punctuation">:</span> <span class="token keyword">Set</span> tempFolder <span class="token operator">=</span> fso<span class="token punctuation">.</span>GetSpecialFolder<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
        <span class="token keyword">Dim</span> tempName <span class="token punctuation">:</span> tempName <span class="token operator">=</span> fso<span class="token punctuation">.</span>GetTempName<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&amp;</span> <span class="token string">&quot;.hta&quot;</span>
        <span class="token keyword">Dim</span> path <span class="token punctuation">:</span> path <span class="token operator">=</span> <span class="token string">&quot;HKCU\Volatile Environment\MsgResp&quot;</span>
        <span class="token keyword">With</span> tempFolder<span class="token punctuation">.</span>CreateTextFile<span class="token punctuation">(</span>tempName<span class="token punctuation">)</span>
            <span class="token punctuation">.</span>Write <span class="token string">&quot;&lt;input type=file name=f&gt;&quot;</span> <span class="token operator">&amp;</span> <span class="token operator">_</span>
            <span class="token string">&quot;&lt;script&gt;f.click();(new ActiveXObject('WScript.Shell'))&quot;</span> <span class="token operator">&amp;</span> <span class="token operator">_</span>
            <span class="token string">&quot;.RegWrite('HKCU\\Volatile Environment\\MsgResp', f.value);&quot;</span> <span class="token operator">&amp;</span> <span class="token operator">_</span>
            <span class="token string">&quot;close();&lt;/script&gt;&quot;</span>
            <span class="token punctuation">.</span>Close
        <span class="token keyword">End</span> <span class="token keyword">With</span>
        <span class="token punctuation">.</span>Run tempFolder <span class="token operator">&amp;</span> <span class="token string">&quot;\&quot;</span> <span class="token operator">&amp;</span> tempName<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token boolean">True</span>
        BrowseForFile <span class="token operator">=</span> <span class="token punctuation">.</span>RegRead<span class="token punctuation">(</span>path<span class="token punctuation">)</span>
        <span class="token punctuation">.</span>RegDelete path
        fso<span class="token punctuation">.</span>DeleteFile tempFolder <span class="token operator">&amp;</span> <span class="token string">&quot;\&quot;</span> <span class="token operator">&amp;</span> tempName
    <span class="token keyword">End</span> <span class="token keyword">With</span>
<span class="token keyword">End</span> <span class="token keyword">Function</span>


<span class="token keyword">Function</span> SelectFolder<span class="token punctuation">(</span><span class="token keyword">default</span><span class="token punctuation">)</span>
    <span class="token keyword">If</span> IsNull<span class="token punctuation">(</span><span class="token keyword">default</span><span class="token punctuation">)</span> <span class="token keyword">Then</span>
        <span class="token keyword">default</span> <span class="token operator">=</span> <span class="token string">&quot;::{20D04FE0-3AEA-1069-A2D8-08002B30309D}&quot;</span>
    <span class="token keyword">End</span> <span class="token keyword">If</span>
    <span class="token keyword">Set</span> Folder <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;Shell.Application&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>BrowseForFolder<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">default</span><span class="token punctuation">)</span>
    <span class="token keyword">If</span> Folder <span class="token keyword">Is</span> <span class="token boolean">Nothing</span> <span class="token keyword">Then</span>
        SelectFolder <span class="token operator">=</span> <span class="token string">&quot;&quot;</span>
    <span class="token keyword">Else</span>
        SelectFolder <span class="token operator">=</span> Folder<span class="token punctuation">.</span>Self<span class="token punctuation">.</span>Path
    <span class="token keyword">End</span> <span class="token keyword">If</span>
<span class="token keyword">End</span> <span class="token keyword">Function</span>
</code></pre></div><h3 id="文件编码转换"><a href="#文件编码转换" class="header-anchor">#</a> 文件编码转换</h3> <ul><li><a href="https://blog.csdn.net/chuhe163/article/details/103549144" target="_blank" rel="noopener noreferrer">VBS处理UTF-8文本之ADODB.stream<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="/files/%E6%96%87%E4%BB%B6%E7%BC%96%E7%A0%81%E8%BD%AC%E6%8D%A2.vbs">文件编码转换.vbs</a></li></ul> <p><strong>转换编码和换行符</strong></p> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token keyword">Set</span> read <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;Adodb.Stream&quot;</span><span class="token punctuation">)</span>
read<span class="token punctuation">.</span><span class="token keyword">Type</span> <span class="token operator">=</span> <span class="token number">2</span>
read<span class="token punctuation">.</span>mode <span class="token operator">=</span> <span class="token number">3</span>
read<span class="token punctuation">.</span>charset <span class="token operator">=</span> <span class="token string">&quot;UTF-8&quot;</span>
read<span class="token punctuation">.</span>Open
read<span class="token punctuation">.</span>loadfromfile fdpath
text <span class="token operator">=</span> read<span class="token punctuation">.</span>ReadText<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
read<span class="token punctuation">.</span>flush
read<span class="token punctuation">.</span>Close
<span class="token keyword">Set</span> save <span class="token operator">=</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;Adodb.Stream&quot;</span><span class="token punctuation">)</span>
save<span class="token punctuation">.</span><span class="token keyword">Type</span> <span class="token operator">=</span> <span class="token number">2</span>
save<span class="token punctuation">.</span>mode <span class="token operator">=</span> <span class="token number">3</span>
save<span class="token punctuation">.</span>charset <span class="token operator">=</span> <span class="token string">&quot;GB2312&quot;</span>
save<span class="token punctuation">.</span>Open
save<span class="token punctuation">.</span>WriteText replace<span class="token punctuation">(</span>text<span class="token punctuation">,</span>vbLf<span class="token punctuation">,</span>vbCrLf<span class="token punctuation">)</span>
save<span class="token punctuation">.</span>SaveToFile fdpath<span class="token punctuation">,</span> <span class="token number">2</span>
save<span class="token punctuation">.</span>flush
save<span class="token punctuation">.</span>Close
</code></pre></div><h2 id="vba"><a href="#vba" class="header-anchor">#</a> VBA</h2> <ul><li><a href="https://docs.microsoft.com/zh-cn/office/vba/api/overview/language-reference" target="_blank" rel="noopener noreferrer">Visual Basic for Applications (VBA) 语言参考<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.zhihu.com/people/xia-xi-lan/posts" target="_blank" rel="noopener noreferrer">VBA学习笔记<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://blog.csdn.net/wordsin/article/details/80575615" target="_blank" rel="noopener noreferrer">xcel之VBA简单宏编程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li>匹配单元格左边英文及其他字符=RegexString(A1,&quot;[^\u4e00-\u9fa5]+&quot;)</li> <li>匹配单元格右边中文及其他字符=RegexString(A1,&quot;[\u4e00-\u9fa5].*&quot;)</li></ul> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token keyword">Function</span> RegexString<span class="token punctuation">(</span>rng <span class="token keyword">As</span> Range<span class="token punctuation">,</span> str <span class="token keyword">As</span> <span class="token keyword">String</span><span class="token punctuation">)</span>
<span class="token comment">'第一个参数rng为区域保持不变， 添加第二个参数str（作为正则表达式）</span>
  <span class="token keyword">With</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;VBscript.regexp&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token keyword">Global</span> <span class="token operator">=</span> <span class="token boolean">True</span>
    <span class="token punctuation">.</span>Pattern <span class="token operator">=</span> str <span class="token comment">'表达式,直接从用户函数的第二个参数中调用</span>
    <span class="token keyword">If</span> <span class="token punctuation">.</span>Execute<span class="token punctuation">(</span>rng<span class="token punctuation">)</span><span class="token punctuation">.</span>Count <span class="token operator">=</span> <span class="token number">0</span> <span class="token keyword">Then</span>
    RegexString <span class="token operator">=</span> <span class="token string">&quot;&quot;</span>
    <span class="token keyword">Else</span>
    RegexString <span class="token operator">=</span> <span class="token punctuation">.</span>Execute<span class="token punctuation">(</span>rng<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token keyword">End</span> <span class="token keyword">If</span>
  <span class="token keyword">End</span> <span class="token keyword">With</span>
<span class="token keyword">End</span> <span class="token keyword">Function</span>
</code></pre></div><ul><li>分割字符串并统计</li></ul> <div class="language-vb extra-class"><pre class="language-vb"><code><span class="token comment">'https://blog.csdn.net/wordsin/article/details/80575615</span>
<span class="token comment">'自定义函数用于工作表时，必须是被动式的，只是返回一个值，不能处理单元格或在工作表上修改，批注是个例外，不能调用range的方法，如：Find，Range.Replace例外</span>
<span class="token keyword">Function</span> ReSplit<span class="token punctuation">(</span>rng <span class="token keyword">As</span> Range<span class="token punctuation">)</span>
    <span class="token keyword">Dim</span> newStr <span class="token keyword">As</span> <span class="token keyword">String</span>
    <span class="token keyword">Dim</span> countNum <span class="token keyword">As</span> <span class="token keyword">Integer</span>
    
    old <span class="token operator">=</span> Strings<span class="token punctuation">.</span>Split<span class="token punctuation">(</span>rng<span class="token punctuation">,</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">For</span> <span class="token keyword">Each</span> e <span class="token keyword">In</span> old
        <span class="token keyword">If</span> e <span class="token operator">&lt;</span><span class="token operator">&gt;</span> <span class="token string">&quot;&quot;</span> <span class="token keyword">Then</span>
            <span class="token comment">'MsgBox TypeName(e)</span>
            <span class="token comment">'Replace(, &quot;/&quot;, &quot;&quot;)</span>
            <span class="token keyword">With</span> CreateObject<span class="token punctuation">(</span><span class="token string">&quot;VBSCRIPT.REGEXP&quot;</span><span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token keyword">Global</span> <span class="token operator">=</span> <span class="token boolean">True</span>
                <span class="token punctuation">.</span>IgnoreCase <span class="token operator">=</span> <span class="token boolean">True</span>
                <span class="token punctuation">.</span>Pattern <span class="token operator">=</span> <span class="token string">&quot;([a-zA-Z]+)([0-9]+)-([0-9]+)&quot;</span>
                <span class="token keyword">If</span> <span class="token punctuation">.</span>test<span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token keyword">Then</span>
                    <span class="token comment">'执行正则表达式，获取子匹配列表</span>
                    <span class="token keyword">Set</span> da <span class="token operator">=</span> <span class="token punctuation">.</span>Execute<span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span>SubMatches
                    last <span class="token operator">=</span> da<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
                    st <span class="token operator">=</span> da<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
                    en <span class="token operator">=</span> da<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
                    <span class="token comment">'Debug.Print last, st, en</span>
                    <span class="token keyword">For</span> i <span class="token operator">=</span> st <span class="token keyword">To</span> en
                        newStr <span class="token operator">=</span> newStr <span class="token operator">&amp;</span> <span class="token string">&quot;,&quot;</span> <span class="token operator">&amp;</span> last <span class="token operator">&amp;</span> i
                        countNum <span class="token operator">=</span> countNum <span class="token operator">+</span> <span class="token number">1</span>
                    <span class="token keyword">Next</span>
                <span class="token keyword">Else</span>
                    newStr <span class="token operator">=</span> newStr <span class="token operator">&amp;</span> <span class="token string">&quot;,&quot;</span> <span class="token operator">&amp;</span> e
                    countNum <span class="token operator">=</span> countNum <span class="token operator">+</span> <span class="token number">1</span>
                <span class="token keyword">End</span> <span class="token keyword">If</span>
            <span class="token keyword">End</span> <span class="token keyword">With</span>
        <span class="token keyword">End</span> <span class="token keyword">If</span>
    <span class="token keyword">Next</span>
    <span class="token keyword">If</span> InStr<span class="token punctuation">(</span>newStr<span class="token punctuation">,</span> <span class="token string">&quot;,&quot;</span><span class="token punctuation">)</span> <span class="token keyword">Then</span>
        newStr <span class="token operator">=</span> Right<span class="token punctuation">(</span>newStr<span class="token punctuation">,</span> Len<span class="token punctuation">(</span>newStr<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">End</span> <span class="token keyword">If</span>
    Debug<span class="token punctuation">.</span>Print newStr
    Debug<span class="token punctuation">.</span>Print countNum
    
    ReSplit <span class="token operator">=</span> newStr
    
    <span class="token comment">'ActiveCell.Address '这是当前单元格地址</span>
    <span class="token comment">'Selection.Offset(1, 0).Select '这是向下跳1格</span>
    <span class="token comment">'Selection.Offset(-1, 0).Select '这是向上跳1格</span>
    <span class="token comment">'Selection.Offset(0, -1).Select '这是向左跳1格</span>
    <span class="token comment">'Selection.Offset(0, 1).Select '这是向右跳1格</span>

<span class="token keyword">End</span> <span class="token keyword">Function</span>


<span class="token keyword">Function</span> SplitCount<span class="token punctuation">(</span>rng <span class="token keyword">As</span> Range<span class="token punctuation">,</span> delimiter <span class="token keyword">As</span> <span class="token keyword">String</span><span class="token punctuation">)</span>
   SplitCount <span class="token operator">=</span> Len<span class="token punctuation">(</span>Strings<span class="token punctuation">.</span>Split<span class="token punctuation">(</span>rng<span class="token punctuation">,</span> delimiter<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">End</span> <span class="token keyword">Function</span>

<span class="token keyword">Sub</span> SetValue<span class="token punctuation">(</span>offset <span class="token keyword">As</span> Range<span class="token punctuation">,</span> value<span class="token punctuation">)</span>
    offset <span class="token operator">=</span> value
<span class="token keyword">End</span> <span class="token keyword">Sub</span>


<span class="token keyword">Sub</span> run<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">Set</span> rng <span class="token operator">=</span> Application<span class="token punctuation">.</span>InputBox<span class="token punctuation">(</span>prompt<span class="token punctuation">:</span><span class="token operator">=</span><span class="token string">&quot;请选择区域&quot;</span><span class="token punctuation">,</span> <span class="token keyword">Type</span><span class="token punctuation">:</span><span class="token operator">=</span><span class="token number">8</span><span class="token punctuation">)</span>
    <span class="token keyword">If</span> rng<span class="token punctuation">.</span>Count <span class="token operator">=</span> <span class="token number">0</span> <span class="token keyword">Then</span>
        MsgBox <span class="token string">&quot;请至少选择一个单元格！&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">,</span> <span class="token string">&quot;提示&quot;</span><span class="token punctuation">:</span>
        <span class="token keyword">Exit</span> <span class="token keyword">Sub</span>
    <span class="token keyword">End</span> <span class="token keyword">If</span>
    <span class="token comment">'If rng.Count &lt;&gt; 1 Then</span>
        <span class="token comment">'MsgBox &quot;只能选择一个单元格！&quot;, , &quot;提示&quot;:</span>
        <span class="token comment">'Exit Sub</span>
    <span class="token comment">'End If</span>
    Debug<span class="token punctuation">.</span>Print <span class="token string">&quot;当前选择：&quot;</span><span class="token punctuation">,</span> rng<span class="token punctuation">.</span>Address<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
    
    rngs <span class="token operator">=</span> Strings<span class="token punctuation">.</span>Split<span class="token punctuation">(</span>rng<span class="token punctuation">.</span>Address<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;:&quot;</span><span class="token punctuation">)</span>
    st <span class="token operator">=</span> Strings<span class="token punctuation">.</span>Split<span class="token punctuation">(</span>rngs<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;$&quot;</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
    sta <span class="token operator">=</span> Replace<span class="token punctuation">(</span>rngs<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;$&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span>
    <span class="token comment">'Debug.Print rngs(0), st, sta</span>
    
    of1Content <span class="token operator">=</span> <span class="token string">&quot;整理后的数据&quot;</span>
    of2Content <span class="token operator">=</span> <span class="token string">&quot;整理后的统计&quot;</span>
    <span class="token keyword">If</span> Range<span class="token punctuation">(</span>st <span class="token operator">&amp;</span> <span class="token string">&quot;1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>offset<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span><span class="token operator">&gt;</span> of1Content <span class="token keyword">Then</span>
        <span class="token comment">'插入空列</span>
        Range<span class="token punctuation">(</span>sta<span class="token punctuation">)</span><span class="token punctuation">.</span>offset<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>EntireColumn<span class="token punctuation">.</span>Insert
        Range<span class="token punctuation">(</span>st <span class="token operator">&amp;</span> <span class="token string">&quot;1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>offset<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=</span> of1Content
    <span class="token keyword">End</span> <span class="token keyword">If</span>
    <span class="token keyword">If</span> Range<span class="token punctuation">(</span>st <span class="token operator">&amp;</span> <span class="token string">&quot;1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>offset<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span><span class="token operator">&gt;</span> of2Content <span class="token keyword">Then</span>
        Range<span class="token punctuation">(</span>sta<span class="token punctuation">)</span><span class="token punctuation">.</span>offset<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span>EntireColumn<span class="token punctuation">.</span>Insert
        Range<span class="token punctuation">(</span>st <span class="token operator">&amp;</span> <span class="token string">&quot;1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>offset<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">=</span> of2Content
    <span class="token keyword">End</span> <span class="token keyword">If</span>
    
    <span class="token keyword">For</span> <span class="token keyword">Each</span> im <span class="token keyword">In</span> rng
        
        <span class="token keyword">If</span> im <span class="token operator">&lt;</span><span class="token operator">&gt;</span> <span class="token string">&quot;&quot;</span> <span class="token keyword">Then</span>
            <span class="token comment">'Debug.Print TypeName(im), im.Address</span>
            
            str1 <span class="token operator">=</span> ReSplit<span class="token punctuation">(</span>Range<span class="token punctuation">(</span>Replace<span class="token punctuation">(</span>im<span class="token punctuation">.</span>Address<span class="token punctuation">,</span> <span class="token string">&quot;$&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            im<span class="token punctuation">.</span>offset<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=</span> str1
            im<span class="token punctuation">.</span>offset<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">=</span> Application<span class="token punctuation">.</span>CountA<span class="token punctuation">(</span>Strings<span class="token punctuation">.</span>Split<span class="token punctuation">(</span>str1<span class="token punctuation">,</span> <span class="token string">&quot;,&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">End</span> <span class="token keyword">If</span>
    <span class="token keyword">Next</span>
<span class="token keyword">End</span> <span class="token keyword">Sub</span>
</code></pre></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/woytu/notes-vuepress/edit/master//Shell/WindowsVBScript.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2021-07-16 10:59:24</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/Shell/WindowsScript.html" class="prev">
        WindowsScript
      </a></span> <!----></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.269009d6.js" defer></script><script src="/assets/js/3.974b9720.js" defer></script><script src="/assets/js/64.aa854ed7.js" defer></script>
  </body>
</html>
